package com.ctshk.rpc.starvip.service.impl;

import com.alibaba.fastjson.JSON;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.HttpUtils;
import com.ctshk.rpc.starvip.dto.start_vip.SiebelMessage;
import com.ctshk.rpc.starvip.dto.start_vip.transaction.LoyTransaction;
import com.ctshk.rpc.starvip.dto.start_vip.transaction.TransactionDTO;
import com.ctshk.rpc.starvip.dto.start_vip.transaction.TransactionQueryDTO;
import com.ctshk.rpc.starvip.factory.transaction.TransactionQueryReqFactory;
import com.ctshk.rpc.starvip.factory.transaction.TransactionReqFactory;
import com.ctshk.rpc.starvip.req.transaction.StarVipTransactionQueryReq;
import com.ctshk.rpc.starvip.req.transaction.StarVipTransactionReq;
import com.ctshk.rpc.starvip.service.IStarVipTransactionService;
import com.ctshk.rpc.starvip.util.JaxbUtil;
import com.ctshk.rpc.starvip.util.ReqUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @BelongsProject: ctshk-cloud
 * @BelongsPackage: com.ctshk.rpc.starvip.service.impl
 * @Author: hef
 * @CreateTime: 2021-04-14 09:40
 * @Description: 描述
 */
@DubboService
public class StarVipTransactionServiceImpl implements IStarVipTransactionService {
    @Override
    public Result transaction(StarVipTransactionReq req) {
        return transaction(req, true);
    }

    @Override
    public Result<LoyTransaction> cancelTransaction(StarVipTransactionReq req) {
        return transaction(req, false);
    }

    /**
     * 执行交易代码
     * @param req
     * @param b true交易 false取消交易
     * @return
     */
    private Result transaction(StarVipTransactionReq req, boolean b) {
        TransactionReqFactory factory = new TransactionReqFactory(req,b);
        // 本地测试打开注释 start
        /*String result = null;
        JaxbUtil jaxbUtil = new JaxbUtil(factory.produce().getClass());
        String sendMessage = jaxbUtil.toXml(factory.produce(), "UTF-8");
        System.out.println(sendMessage);
        try {
            result = HttpUtils.xmlSendByPost("http://47.115.21.70:9000/app/user/vip/testXml",sendMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);*/
        // 本地测试打开注释 end
        String result = ReqUtil.sendReq(factory);
        TransactionDTO dto = ReqUtil.fromXml(TransactionDTO.class, result);
        if (Objects.nonNull(dto)) {
            if (Objects.nonNull(dto.getListOfLoyEaiTransaction())
                    && Objects.nonNull(dto.getListOfLoyEaiTransaction().getLoyTransaction())) {
                return Result.success(dto.getListOfLoyEaiTransaction().getLoyTransaction());
            }else if(Objects.nonNull(dto.getErrorInfo())){
                throw new BusinessException(SystemError.STAR_VIP_20000, dto.getErrorInfo().getErrorMessage());
            }
        }
        return Result.failed(SystemError.SYS_437);
    }

    @Override
    public Result<TransactionQueryDTO> queryTransaction(StarVipTransactionQueryReq req) {
        TransactionQueryReqFactory factory = new TransactionQueryReqFactory(req);
        // 本地测试打开注释 start
        /*String result = null;
        JaxbUtil jaxbUtil = new JaxbUtil(factory.produce().getClass());
        String sendMessage = jaxbUtil.toXml(factory.produce(), "UTF-8");
        System.out.println(sendMessage);
        try {
            result = HttpUtils.xmlSendByPost("http://47.115.21.70:9000/app/user/vip/testXml",sendMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);*/
        // 本地测试打开注释 end
        String result = ReqUtil.sendReq(factory);
        TransactionQueryDTO dto = ReqUtil.fromXml(TransactionQueryDTO.class, result);
        if (Objects.nonNull(dto)) {
            if(Objects.nonNull(dto.getErrorInfo())){
                throw new BusinessException(SystemError.STAR_VIP_20000, dto.getErrorInfo().getErrorMessage());
            }
            return Result.success(dto);
        }
        return Result.failed(SystemError.SYS_437);
    }

}
